package com.zy.qudadid.presenter;

//import com.zhy.http.okhttp.OkHttpUtils;
//import com.zhy.http.okhttp.callback.StringCallback;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import com.zy.qudadid.model.User;
import com.zy.qudadid.ui.view.OwnInfoView;
import com.zy.qudadid.model.Res;
import com.zy.qudadid.network.Const;
import com.zy.qudadid.network.Net;
import com.zy.qudadid.presenter.base.BasePresenterImp;
import com.zy.qudadid.utils.ToolsUtil;

import java.io.File;

import okhttp3.Call;
import okhttp3.Request;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by ASUS on 2017/3/6.
 */

public class OwnInfoPresenter extends BasePresenterImp<OwnInfoView> {

    public void changeAvatar(String phoneNumber, String id, String path) {
        String name = path.substring(path.lastIndexOf("/") + 1);
        File file = new File(path);

        OkHttpUtils
                .post()
                .url(Const.BASE_URL + "api.php/User/passenger_avatar")
                .addFile("imgsrc", name, file)
                .addParams("mobile", phoneNumber)
                .addParams("id", id)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int i) {
                   //     request.toString();
                        ToolsUtil.print("OwnInfoPresenter","上传失败");
                    }

                    @Override
                    public void onResponse(String response,int i) {
                        response.toString();
                    //    ToolsUtil.print("-----","上传成功");
                        ToolsUtil.print("-----",response.toString());
                        view.successChangeAvatar();
                    }
                });
    }

    public void ChangeInfo(String data){
        Subscription subscription = Net.getService()
                .ChangeInfo(data)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Res>() {

                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(Res res) {
                        int num = 0;
                        if (res.code == Const.OK) {
                            view.successChangeOwnInfo();
                        }
                    }
                });

        addSubscription(subscription);
    }

    public void InquireInfo(String data){
        Subscription subscription = Net.getService()
                .InquireInfo(data)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Res<User>>() {

                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(Res<User> res) {
                        int num = 0;
                        if (res.code == Const.OK) {
                            view.successInquireInfo(res.datas);
                        }
                    }
                });

        addSubscription(subscription);
    }
}
